package com.bole.controller;

import com.alibaba.fastjson.JSONObject;
import com.bole.constant.SmsConstant;
import com.bole.data.Result;
import com.bole.data.ResultEnum;
import com.bole.dto.brand.DicBrandDto;
import com.bole.dto.car.TbCarDto;
import com.bole.dto.user.UserDto;
import com.bole.feign.reception.UserServiceFeign;
import com.bole.utils.CookieUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.misc.Contended;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
@CrossOrigin
@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private UserServiceFeign userServiceFeign;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("toList")
    @ApiOperation(value = "跳转注册")
    public String toList() {
        return "会员中心";

    }


    @GetMapping("toLogin")
    @ApiOperation(value = "登录逻辑")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "telephone", value = "账号", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "noteCode", value = "验证码", required = true, dataTypeClass = String.class)
    })
    @ResponseBody
    public Result toLogin(HttpServletRequest request, HttpServletResponse response, @RequestParam("telephone") String telephone, @RequestParam("noteCode") String noteCode) {
        if (StringUtils.isBlank(telephone)) {
            return Result.response(ResultEnum.FAIL).setData("手机号不能为空");
        }
        if (StringUtils.isBlank(noteCode)) {
            return Result.response(ResultEnum.FAIL).setData("验证码不能为空");
        }
        String code = (String) redisTemplate.opsForHash().get(SmsConstant.USER_REGISTER_PHONENUMBER, telephone);
        if (!code.equals(noteCode)) {
            return Result.response(ResultEnum.FAIL).setData("验证码不一致");
        }
        //获取subject主体
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(telephone, "123");
        //执行认证逻辑
        try {
            //执行认证逻辑
            subject.login(token);
           CookieUtils.setCookie(request,response,"LOGIN_TELEPHONE",telephone);
           String login_telephone = CookieUtils.getCookieValue(request, "LOGIN_TELEPHONE");
            System.out.println("cookie手机号"+login_telephone);
            return Result.response(ResultEnum.OK);
        } catch (UnknownAccountException e) {
            System.out.println("账号错误");
            return Result.response(ResultEnum.FAIL);
        } catch (IncorrectCredentialsException e) {
            System.out.println("密码错误");
            e.printStackTrace();
            return Result.response(ResultEnum.FAIL);
        }

    }


    @RequestMapping("toUser")
    @ApiOperation(value = "跳转账户管理")
    public String toUser() {
        return "会员中心_账户管理";

    }


    //获取会员信息
    @PostMapping("selectUserByTelephone")
    @ApiOperation(value = "查询")
    @ResponseBody
    public Result selectUserByTelephone() {
            //获取用户登录信息
            UserDto user = (UserDto) SecurityUtils.getSubject().getPrincipal();
            System.out.println(user.getId());
            if (user == null) {
                return Result.response(ResultEnum.FAIL);
            } else {
                UserDto userDto = userServiceFeign.selectUserByTelephone(user.getId());
                return Result.response(ResultEnum.OK).setData(userDto);
            }
    }

   //完善会员信息
    @PostMapping("addUserInfo")
    @ApiOperation(value = "增加会员信息")
    @ApiImplicitParam(name = "userDto",value = "会员对象",required = true,dataTypeClass = UserDto.class)
    @ResponseBody
    public Result addBrandInfo(@RequestBody UserDto userDto){
        userServiceFeign.addUserInfo(userDto);
        return Result.response(ResultEnum.OK);
    }

    /**
     * 判断是否登录
     * @return
     */
    @GetMapping("isLogin")
    @ApiOperation(value = "判断是否登录")
    @ResponseBody
    public Result isLogin() {
        Subject subject = SecurityUtils.getSubject();
        UserDto userDto = (UserDto) subject.getPrincipal();
        System.out.println("islogin"+userDto);
        if (null == userDto) {
            return Result.fail().setCode(409).setData("请先登录");
        } else {
            return Result.ok().setData("已经登录");
        }
    }


    /**
     * 判断是否登录
     * @return
     */
    @GetMapping("isLoginByInfo")
    @ApiOperation(value = "判断是否登录")
    @ResponseBody
    public Result isLoginByInfo(HttpServletRequest request) {
        String loginTelephone = CookieUtils.getCookieValue(request, "LOGIN_TELEPHONE");
        System.out.println("手机号"+loginTelephone);
        if (null == loginTelephone || "".equals(loginTelephone)) {
            return Result.fail().setCode(409).setData("请先登录");
        } else {
            return Result.ok().setData("已经登录");
        }
    }

}






